let App = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    xin: '☰',
    isLoading: true, // 是否正在加载中
    page: 1, // 当前页码
    checkDate: '', //选择的时间
    scrollHeight: 0,
    list: [],
    onmore:true,
    scrollTop: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var today = new Date();
    var iday = today.getFullYear() * 10000 + (today.getMonth() + 1) * 100 + today.getDate();
    var week = getweek(today.getUTCDay())
    var year = today.getFullYear()
    var month = today.getMonth() + 1
    var day = today.getDate()


    that.setData({
      date:  year + '年' + month + '月' + day + '日  ' + week,
      starttimes: year + '-' + month + '-' + day,
      checkDate: year + '-' + month + '-' + day
    });
    that.setListHeight();

  },
  datePickerBindchange: function (e) {
    var today = new Date(e.detail.value)
    var week = getweek(today.getUTCDay())
    var year = today.getFullYear()
    var month = today.getMonth() + 1
    var day = today.getDate()
    var iday = today.getFullYear() * 10000 + (today.getMonth() + 1) * 100 + today.getDate();
    this.setData({
      date: year + '年' + month + '月' + day + '日  ' + week,
      starttimes: year + '-' + month + '-' + day,
    })
  },

  goTop: function(t) {
    this.setData({
      scrollTop: 0
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  //生命周期函数--监听页面初次渲染完成
  onReady: function (e) {
    var that = this;
    //请求数据
    that.getList()
  },

  /**
   * 下拉到底加载数据
   */
  onDownLoad() {
    console.log('fuck you')
    let _this = this;

    // 加载下一页列表
    _this.getList(true, ++_this.data.page);
   // 已经是最后一页
   if (_this.data.page >= _this.data.list.last_page) {
     _this.setData({
       noMore: true
     });
     return false;
   }
   
  },

  nono: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
     this.setData({
       onmore:true,
       page:1
     })
      this.getList();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if(this.data.onmore){
      this.getList(true,++this.data.page)
    }
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  getList(isPage, pageNum) {
    let _this = this;
    App._get('study/subtract_list', {
      page: pageNum || 1,
      date: _this.data.checkDate
      // date: '2021-03-04'
    }, result => {
      if(result.data.length==0){
        _this.setData({
          onmore:false
        })
        return false;
      }
      let resList = result.data,
        dataList = _this.data.list;
      if (isPage == true) {

        _this.setData({
          list: dataList.concat(resList),
          isLoading: false,
        });
      } else {
        _this.setData({
          list: resList,
          isLoading: false,
        });
      }
    });
  },
    /**
   * 设置分类列表高度
   */
  setListHeight() {
    let _this = this;
    wx.getSystemInfo({
      success(res) {
        _this.setData({
          scrollHeight: res.windowHeight - 47,
        });
      }
    });
  }

})

function getweek(week) {
  var iweek;
  switch (week) {
    case 1:
      iweek = "星期一";
      break;
    case 2:
      iweek = "星期二";
      break;
    case 3:
      iweek = "星期三";
      break;
    case 4:
      iweek = "星期四";
      break;
    case 5:
      iweek = "星期五";
      break;
    case 6:
      iweek = "星期六";
      break;
    case 0:
      iweek = "星期日";
  }
  return iweek
}